package Operaciones;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Clases.*;
import BasedeDatos.*;

public class Control {
	Connection con;
	
	public void banusuario() throws SQLException{
		con = Conexion.conexion();
		Statement stmt = con.createStatement();
		Statement stmt2 = con.createStatement();
		String q = "REPLACE INTO baneados SELECT guid,motivo FROM porbanear;";
		String q2 = "DELETE FROM porbanear;";
		stmt.executeUpdate(q);
		stmt2.executeUpdate(q2);
		
	}
	
	public void paraban(int guid, String motivo) throws SQLException{
		con = Conexion.conexion();
		Statement stmt = con.createStatement();
		String q = "REPLACE INTO porbanear VALUES ('"+guid+"','"+motivo+"');";
		stmt.executeUpdate(q);
	}
	
	
	public void modPoder(String user, int nivel) throws SQLException{
		con = Conexion.conexion();
		Statement stmt = con.createStatement();
		if(nivel>0){
		String q = "REPLACE INTO usuarioacceso SELECT guid,'"+nivel+"','','0' FROM usuario WHERE user = '"+user+"';";
		stmt.executeUpdate(q);
		}
		else{
			String q = "DELETE FROM usuarioacceso WHERE guid IN (SELECT guid FROM usuario WHERE user = '"+user+"');";
			stmt.executeUpdate(q);
		}
	}
	
	public void resetsol() throws SQLException{
		con = Conexion.conexion();
		Statement stmt = con.createStatement();
		String q = "DELETE FROM spellasignado WHERE reparada = 1;";
		String q2 = "DELETE FROM npcasignado WHERE reparada = 1;";
		String q3 = "DELETE FROM misionasignado WHERE reparada = 1;";
		String q4 = "DELETE FROM itemasignado WHERE reparada = 1;";
		String q5 = "DELETE FROM spell WHERE asignado = 1;";
		String q6 = "DELETE FROM mision WHERE asignado = 1;";
		String q7 = "DELETE FROM item WHERE asignado = 1;";
		String q8 = "DELETE FROM npc WHERE asignado = 1;";
		stmt.executeUpdate(q);
		stmt.executeUpdate(q2);
		stmt.executeUpdate(q3);
		stmt.executeUpdate(q4);
		stmt.executeUpdate(q5);
		stmt.executeUpdate(q6);
		stmt.executeUpdate(q7);
		stmt.executeUpdate(q8);
	}
	
	public int selectguidnuser(String user) throws SQLException{
		int res = 0;
		con = Conexion.conexion();
		Statement stmt = con.createStatement();
		String q = "SELECT guid FROM usuario WHERE nuser = '"+user+"';";
		try{
			ResultSet rs = stmt.executeQuery(q);
			while(rs.next()){
				res = rs.getInt("guid");
			}
			rs.close();
		}
		catch(SQLException e){}
		return res;
	}
}
